package model.object;

import java.awt.Rectangle;
import java.util.Random;

import javax.swing.JPanel;

import model.general.Location;
import model.move.MoveItems;
import model.observer.Obsever;
import model.option.Option;

public class Fish extends ObjectAnimate implements Obsever {
	private Option option;
	private Hook hook;
	private boolean isMove = true;
	private boolean isStandLeft;

	public Fish(int x, int y, String srcImg, Option option, JPanel pnl) {
		super(x, y, srcImg);
		// TODO Auto-generated constructor stub
		this.move = new MoveItems(location);
		this.option = option;
		this.hook = Hook.getInstance();
		this.pnl = pnl;
		findOrientation(x);
	}

	public boolean findOrientation(int x) {
		System.out.println("x "+x);
		System.out.println("pnl.getX() + (pnl.getWidth() / 2) "+pnl.getX() + (pnl.getWidth() / 2));
		
		if (x < (pnl.getX() + (pnl.getWidth() / 2))) {
			isStandLeft = true;
		} else {
			isStandLeft = false;
		}
		System.out.println("isl "+isStandLeft);
		return isStandLeft;
	}

	@Override
	public void move() {
		if (isMove) {
			System.out.println("before fish move " + location);
			if (hasOutPanel()) {
				System.out.println("before reset "+location);
				reset();
				System.out.println("after reset "+location);
				System.out.println(" ");
			}
			System.out.println("isStandleft "+isStandLeft);
			if (isStandLeft) {
				move.moveRight();
			} else {
				move.moveLeft();
			}
			System.out.println("after fish move " + location);
		} else {
			move.stop();
		}

	}

	@Override
	public void updateObject() {

	}

	/**
	 * check xem la minh da bi bat chua va lam cai gi do?
	 */
	@Override
	public void update() {
		// neu da bi bat thi....
		if (isCatched()) {
			isMove = false;
			hook.catchItems(this);
		}
	}

	/**
	 * kiem tra xem minh bi bat chua
	 * 
	 * @return boolean
	 */
	public boolean isCatched() {
		Rectangle rectFish = this.getBounds();
		Rectangle rectHook = hook.getBounds();
		if (rectFish.intersects(rectHook)) {
			return true;
		}
		return false;
	}

	@Override
	public void updateLocation(Location loc) {
		this.location = loc;
	}

	@Override
	public void reset() {
		this.location = randLocation();
		System.out.println("reset fish " + location);
		findOrientation(location.getX());
		isMove = true;
	}

	@Override
	public boolean hasOutPanel() {
		int min = this.getPnl().getX() - this.width;
		int max = this.pnl.getX() + this.pnl.getWidth() + this.width;
		System.out.println("max "+max);
		System.out.println("min "+min);
		System.out.println("location "+location);
		
		if ((location.getX() > min) || (location.getX() < max)) {
			System.out.println("has out false");
			return false;
		}
		System.out.println("has out true");
		return true;
	}

	public Location randLocation() {
		Random rand = new Random();
		int m = rand.nextInt(2);
		int x;
		if (m <= 1) {
			x = (this.pnl.getX() - this.width)+1;
		} else {
			x = this.pnl.getX() + this.pnl.getWidth()-1;
		}
		int y = rand.nextInt(this.pnl.getY()+this.pnl.getHeight());
		return new Location(x, y);
	}
}
